﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LarDosIdosos
{
    public partial class Visitante : System.Web.UI.Page
    {
        private Business.Model.Funcionario funcionario;
        private const string Acesso = "acesso";
        private const string Editar = "editar";

        protected void Page_Load(object sender, EventArgs e)
        {
            CarregaFuncionario();
            if (!ValidaPermissao("acesso"))
            {
                Response.Redirect("~/Default.aspx");
            }
            CarregaBotoesDeCadastro();
        }

        private void CarregaBotoesDeCadastro()
        {
            //buttonCadastrar.Visible = ValidaPermissao(Editar);
        }

        private void CarregaFuncionario()
        {
            if (Session["usuario"] != null)
            {
                funcionario = (Business.Model.Funcionario)Session["usuario"];
            }
        }

        //Note: Valida se é assistente, médico ou téc de enfermagem
        private bool ValidaPermissao(string tipo)
        {
            if (funcionario != null)
            {
                switch (tipo)
                {
                    case Acesso:
                        if (funcionario.Cargo_Funcionario.CodigoCargo == 1)
                        {
                            return true;
                        }
                        break;
                    case Editar:
                        if (funcionario.Cargo_Funcionario.CodigoCargo == 1)
                        {
                            return true;
                        }
                        break;
                }
            }
            return false;
        }
		
		protected void buttonConsulta_OnClick(object sender, EventArgs e)
		{
			string busca = textBoxConsulta.Text;
			if(busca != "")
			{
				dataSourceVisitante.SelectParameters.Clear();
				Parameter param = new Parameter();
				param.Name = "sql";
				param.DefaultValue = "select * from visitante where nome_visitante like '%" + busca + "%'";
				dataSourceVisitante.SelectParameters.Add(param);
				//dataSourceResponsavel.SelectParameters.Add(
				//List<Business.Model.Responsavel> responsaveis = dataSourceResponsavel.BuscaResponsaveis(0,10,"","select * from responsavel where nome_responsavel like '%" + busca + "%'");
				GridView grid = (GridView) updatePanelVisitante.FindControl("gridViewVisitante");
				grid.DataBind();
			}
		}

        protected void gridViewVisitante_DataBound(object sender, EventArgs e)
        {
            
        }

        protected void gridViewVisitante_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
            {
                ImageButton editar = (ImageButton)e.Row.FindControl("imageButtonEditar");
                ImageButton excluir = (ImageButton)e.Row.FindControl("imageButtonExcluir");
                editar.Visible = excluir.Visible = ValidaPermissao(Editar);
            }
        }
		

        protected void gridViewVisitante_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "visualizar")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gridViewVisitante.Rows[index];
                HiddenField hiddenField = (HiddenField)row.FindControl("hiddenFieldId");
                if (hiddenField != null)
				{
					//MontaPaginaComDetalhes(Convert.ToInt32(hiddenField.Value), false);
				}

            }
            if (e.CommandName == "editar")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gridViewVisitante.Rows[index];
                HiddenField hiddenField = (HiddenField)row.FindControl("hiddenFieldId");
                if (hiddenField != null)
				{
					//MontaPaginaComDetalhes(Convert.ToInt32(hiddenField.Value), true);
				}

            }
            if (e.CommandName == "excluir")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gridViewVisitante.Rows[index];
                HiddenField hiddenField = (HiddenField)row.FindControl("hiddenFieldId");
                if (hiddenField != null)
				{
					//ExcluiRegistro(Convert.ToInt32(hiddenField.Value));
				}
            }
        }
		
		protected void gridViewVisitante_OnSorted(object sender, EventArgs e)
        {
            
        }

        protected void gridViewVisitante_OnPageIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void ddPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
		
		protected void linkButtonVoltar_OnClick(object sender, EventArgs e)
        {
            
        }


        protected void buttonCadastrar_OnClick(object sender, EventArgs e)
        {
            
        }
    }
}